package com.crawl.proxy.site.ip181;


import com.crawl.proxy.ProxyListPageParser;
import com.crawl.proxy.entity.Proxy;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import static com.crawl.core.util.Constants.TIME_INTERVAL;

/**
 * http://www.ip181.com/
 */
public class Ip181ProxyListPageParser implements ProxyListPageParser {
    /**
     * 根据http://www.ip181.com/网页内容解析出proxy list
     * @param content 网页内容
     * @return proxy list
     */
    @Override
    public List<Proxy> parse(String content) {
        //根据网页内容创建Document对象
        Document document = Jsoup.parse(content);
        //获取table标签下索引大于0的tr标签
        Elements elements = document.select("table tr:gt(0)");
        List<Proxy> proxyList = new ArrayList<>(elements.size());
        for (Element element : elements){
            //获取第一个td标签，text为ip
            String ip = element.select("td:eq(0)").first().text();
            //获取第2个td标签，text为port
            String port  = element.select("td:eq(1)").first().text();
            //获取第3个td标签，text为匿名标志
            String isAnonymous = element.select("td:eq(2)").first().text();
            if(!anonymousFlag || isAnonymous.contains("匿")){
                //添加匿名代理至proxyList
                proxyList.add(new Proxy(ip, Integer.valueOf(port), TIME_INTERVAL));
            }
        }
        return proxyList;
    }
}
